package com.ymttest.database.sharding.db.sqlwapper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionUtils;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Iterables;
import com.ymt.core.tool.Logger;
import com.ymttest.database.sharding.config.DbExtSourceConfig;
import com.ymttest.database.sharding.db.ext.mapper.ActualDeleteDeliveryMapper;
import com.ymttest.database.sharding.db.ext.mapper.DeliveryMapper;
import com.ymttest.database.sharding.db.ext.mapper.FinishActivityMapper;
import com.ymttest.database.sharding.db.ext.mapper.IdcardBlackListMapper;
import com.ymttest.database.sharding.db.ext.mapper.IdcardCostMapper;
import com.ymttest.database.sharding.db.ext.mapper.OrderAutoAcceptActivityDetailMapper;
import com.ymttest.database.sharding.db.ext.mapper.OrderDetailExtMapper;
import com.ymttest.database.sharding.db.ext.mapper.OrderExtMapper;
import com.ymttest.database.sharding.db.ext.mapper.OrderSyncCmdExtMapper;
import com.ymttest.database.sharding.db.ext.mapper.PickUpCodeMapper;
import com.ymttest.database.sharding.db.ext.model.ActualDeleteDeliveryExample;
import com.ymttest.database.sharding.db.ext.model.ActualDeleteDeliveryPo;
import com.ymttest.database.sharding.db.ext.model.DeliveryExample;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.ext.model.FinishActivityExample;
import com.ymttest.database.sharding.db.ext.model.FinishActivityPo;
import com.ymttest.database.sharding.db.ext.model.IdcardBlackListExample;
import com.ymttest.database.sharding.db.ext.model.IdcardBlackListPo;
import com.ymttest.database.sharding.db.ext.model.IdcardCostExample;
import com.ymttest.database.sharding.db.ext.model.IdcardCostPo;
import com.ymttest.database.sharding.db.ext.model.OrderAutoAcceptActivityDetailExample;
import com.ymttest.database.sharding.db.ext.model.OrderAutoAcceptActivityDetailPo;
import com.ymttest.database.sharding.db.ext.model.OrderDetailExtExample;
import com.ymttest.database.sharding.db.ext.model.OrderDetailExtPo;
import com.ymttest.database.sharding.db.ext.model.OrderExtExample;
import com.ymttest.database.sharding.db.ext.model.OrderExtPo;
import com.ymttest.database.sharding.db.ext.model.PickUpCodeExample;
import com.ymttest.database.sharding.db.ext.model.PickUpCodePo;
import com.ymttest.database.sharding.db.ext.query.CommandExtQuery;

/************************************************************************************
  新交易 非分库分表  查询wapper
 ************************************************************************************/
public class OrderWapper {

	public static SqlSession sqlSession;

	/**
	 * 订单扩展表(order_ext)
	 */
	public OrderExtMapper orderExtMapper;

	public OrderExtExample orderExtExample;

	/**
	 * 订单商品扩展表(order_detail_ext)
	 */
	public OrderDetailExtMapper orderDetailExtMapper;

	public OrderDetailExtExample orderDetailExtExample;

	/**
	 * 身份证限额表(idcard_cost)
	 */
	public IdcardCostMapper idcardCostMapper;

	public IdcardCostExample idcardCostExample;

	/**
	 * 身份证限额黑名单表(idcard_blacklist)
	 */
	public IdcardBlackListMapper idcardBlackListMapper;

	public IdcardBlackListExample idcardBlackListExample;

	/**
	 * order_sync_cmd_ext(订单状态操作同步指令)
	 */
	public OrderSyncCmdExtMapper orderSyncCmdExtMapper;

	/**
	 * 同步查询
	 */
	public CommandExtQuery commandExtMapper;

	/**
	 * 订单物流发货(delivery)
	 */
	public DeliveryMapper deliveryMapper;

	public DeliveryExample deliveryExample;
	
	/**
	 * 订单自动接单活动详细表(order_auto_accept_activity_detail)
	 */
	public OrderAutoAcceptActivityDetailMapper orderAutoAcceptActivityDetailMapper;

	public OrderAutoAcceptActivityDetailExample orderAutoAcceptActivityDetailExample;
	
	/**
	 * 活动结束表(finish_activity)
	 */
	public FinishActivityMapper finishActivityMapper;

	public FinishActivityExample finishActivityExample;
	
	/**
	 * 发货删除表(actual_delete_delivery)
	 */
	public ActualDeleteDeliveryMapper actualDeleteDeliveryMapper;

	public ActualDeleteDeliveryExample actualDeleteDeliveryExample;
	
	/**
	 * 提货码(pick_up_code)
	 */
	public PickUpCodeMapper pickUpCodeMapper;

	public PickUpCodeExample pickUpCodeExample;
	
	public void closeSession() {
		try {
			if(sqlSession!=null&&sqlSession.getConnection()!=null&&!sqlSession.getConnection().isClosed()) {
				sqlSession.close();
				sqlSession=null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public OrderWapper() {
		try {
			if(sqlSession==null || sqlSession.getConnection()==null ||sqlSession.getConnection().isClosed()) {
//				sqlSession = SqlSessionUtils.getSqlSession(DbExtSourceConfig
//					.getInstance().extSessionFactory());
				sqlSession = DbExtSourceConfig
						.getInstance().extSessionFactory().openSession();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		orderExtMapper = sqlSession.getMapper(OrderExtMapper.class);

		orderExtExample = new OrderExtExample();

		orderDetailExtMapper = sqlSession.getMapper(OrderDetailExtMapper.class);

		orderDetailExtExample = new OrderDetailExtExample();

		idcardCostMapper = sqlSession.getMapper(IdcardCostMapper.class);

		idcardCostExample = new IdcardCostExample();

		idcardBlackListMapper = sqlSession
				.getMapper(IdcardBlackListMapper.class);

		idcardBlackListExample = new IdcardBlackListExample();

		commandExtMapper = sqlSession.getMapper(CommandExtQuery.class);

		deliveryMapper = sqlSession.getMapper(DeliveryMapper.class);

		deliveryExample = new DeliveryExample();

		orderAutoAcceptActivityDetailMapper = sqlSession.getMapper(OrderAutoAcceptActivityDetailMapper.class);

		orderAutoAcceptActivityDetailExample = new OrderAutoAcceptActivityDetailExample();
		
		finishActivityMapper = sqlSession.getMapper(FinishActivityMapper.class);

		finishActivityExample = new FinishActivityExample();
		
		actualDeleteDeliveryMapper = sqlSession.getMapper(ActualDeleteDeliveryMapper.class);

		actualDeleteDeliveryExample = new ActualDeleteDeliveryExample();
		
		this.pickUpCodeMapper = sqlSession.getMapper(PickUpCodeMapper.class);

		this.pickUpCodeExample = new PickUpCodeExample();
	}
	
	public void clearCache() {
		if (sqlSession != null) {
			sqlSession.clearCache();
		}
	}
	
	/**	根据订单号查询	订单扩展表(order_ext)
	 * 
	 * @param orderId
	 * @return
	 */
	public OrderExtPo selectOrderExtByOrderId(long orderId) {

		return orderExtMapper.selectByPrimaryKey(orderId);

	}
	/**	根据orderDetailId查询	订单商品扩展表(order_detail_ext)
	 * 
	 * @param orderDetailId
	 * @return
	 */
	public OrderDetailExtPo selectOrderDetailExtByOrderId(long orderDetailId) {

		orderDetailExtExample.clear();

		orderDetailExtExample.createCriteria().andOrderDetailIdEqualTo(
				orderDetailId);

		return Iterables.getFirst(
				orderDetailExtMapper.selectByExample(orderDetailExtExample),
				null);
	}

	/**	根据idCardNo查询	身份证限额表(idcard_cost)
	 * 
	 * @param idCardNo
	 * @return
	 */
	public IdcardCostPo selectIdcardCostByIdCardNo(String idCardNo) {

		idcardCostExample.clear();

		idcardCostExample.createCriteria().andIdcardNoEqualTo(idCardNo);

		return Iterables.getFirst(
				idcardCostMapper.selectByExample(idcardCostExample), null);
	}

	/**	随机查询1个身份证信息
	* 
	* @param idCardNo
	* @return
	*/
	public IdcardCostPo selectIdcardCost() {

		idcardCostExample.clear();

		idcardCostExample.createCriteria().andIdBetween(23280L, 23390L);

		return Iterables.getFirst(
				idcardCostMapper.selectByExample(idcardCostExample), null);
	}
	
	/**	根据idCardNo修改 身份证限额表(idcard_cost)
	 * 
	 * @param idCardNo
	 * @return
	 */
	public void updatIdCardCostByIdCardNo(IdcardCostPo record, String idcardNo) {

		idcardCostExample.clear();

		idcardCostExample.createCriteria().andIdcardNoEqualTo(idcardNo);

		idcardCostMapper.updateByExample(record, idcardCostExample);
	}
	
	/**	根据idCardNo查询	身份证限额黑名单表(idcard_blacklist)
	 * 
	 * @param idCardNo
	 * @return
	 */
	public IdcardBlackListPo selectIdcardBlackListByIdCardNo(String idcardNo) {

		idcardBlackListExample.clear();

		idcardBlackListExample.createCriteria().andIdcardNoEqualTo(idcardNo);

		return Iterables.getFirst(
				idcardBlackListMapper.selectByExample(idcardBlackListExample),
				null);
	}
	
	/**	根据idCardNo修改  身份证限额黑名单表(idcard_blacklist)
	 * 
	 * @param idCardNo
	 * @return
	 */
	public void updatIdCardCostByIdCardNo(IdcardBlackListPo record) {

		idcardBlackListMapper.updateByPrimaryKeySelective(record);
	}

	/**	根据orderId查询	order_sync_cmd(订单状态操作同步指令)
	 * 
	 * @param orderId
	 * @return
	 */
	public List<Map<String, Object>> selectCommandExtByOrderId(long orderId) {

		commandExtMapper.getOrder(orderId);
		return commandExtMapper.getProduct(orderId);
	}
	
	/**	根据orderId修改地址
	 * 
	 * @return
	 */
	public void updateAddressByOrderId(OrderExtPo record) {

		orderExtMapper.updateByPrimaryKey(record);
	}

	/**	
	 * 查询表  delivery  数据，倒序返回第一条数据
	 * @param orderId 
	 * @return	DeliveryPo
	 */
	public DeliveryPo selectDeliveryByOrderId(long orderId) {

		deliveryExample.clear();

		deliveryExample.createCriteria().andOrderIdEqualTo(orderId);
		deliveryExample.setOrderByClause("id DESC");
		return Iterables.getFirst(
				deliveryMapper.selectByExample(deliveryExample), null);
	}
	
	/**	
	 * 查询表  delivery 数据，鉴定中心发货
	 * @param orderId 
	 * @return	DeliveryPo
	 */
	public DeliveryPo selectDeliveryByAuthenticateDelivery(long orderId) {

		deliveryExample.clear();

		deliveryExample.createCriteria().andOrderIdEqualTo(orderId).andRealDeliveryAreaEqualTo(2);
		deliveryExample.setOrderByClause("id DESC");
		return Iterables.getFirst(
				deliveryMapper.selectByExample(deliveryExample), null);
	}
	
	/**	
	 * 查询表  delivery  数据，
	 * @param orderId
	 * @param section  
	 * @return	DeliveryPo
	 */
	public DeliveryPo selectDeliveryByOrderIdAndSection(long orderId, int section) {

		deliveryExample.clear();

		deliveryExample.createCriteria().andOrderIdEqualTo(orderId).andSectionEqualTo(section);
		deliveryExample.setOrderByClause("id DESC");
		return Iterables.getFirst(
				deliveryMapper.selectByExample(deliveryExample), null);
	}
	
	/**	
	 * 查询表  delivery  数据，
	 * @param orderId
	 * @param section  
	 * @return	DeliveryPo
	 */
	public DeliveryPo selectDeliveryByOrderIdAndBillCode(long orderId, String billCode) {

		deliveryExample.clear();

		deliveryExample.createCriteria().andOrderIdEqualTo(orderId).andBillNoEqualTo(billCode);
		deliveryExample.setOrderByClause("id DESC");
		return Iterables.getFirst(
				deliveryMapper.selectByExample(deliveryExample), null);
	}
	
	/**	
	 * 查询表  delivery  数据，返回所有数据
	 * @param orderId 
	 * @return	DeliveryPo
	 */
	public List<DeliveryPo> selectDeliverysByOrderId(long orderId) {

		deliveryExample.clear();
		
		deliveryExample.createCriteria().andOrderIdEqualTo(orderId);
		deliveryExample.setOrderByClause("id ASC");
		return deliveryMapper.selectByExample(deliveryExample);
	}
	
	/**	
	 * 查询表  delivery  数据
	 * @param orderId 
	 * @return	DeliveryPo size
	 */
	public int selectDeliverySizeByOrderId(long orderId) {

		deliveryExample.clear();

		deliveryExample.createCriteria().andOrderIdEqualTo(orderId);
		deliveryExample.setOrderByClause("id DESC");
		return deliveryMapper.selectByExample(deliveryExample).size();
	}
	
	public List<String> selectDeliveryCodeByOrderIdWithDeleted(long orderId) {
		
		List<String> billCodes = new ArrayList<String>();
		
		deliveryExample.clear();
		deliveryExample.createCriteria().andOrderIdEqualTo(orderId);
		
		List<DeliveryPo> deliveryList = deliveryMapper.selectByExample(deliveryExample);
		
		if (!CollectionUtils.isEmpty(deliveryList)) {
			deliveryList.forEach(o -> {
				billCodes.add(o.getBillNo());
			});
		}
		
		return billCodes;
	}
	
	public List<String> selectDeliveryCodeByOrderId(long orderId) {
		
		List<String> billCodes = new ArrayList<String>();
		
		deliveryExample.clear();
		deliveryExample.createCriteria().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
		
		List<DeliveryPo> deliveryList = deliveryMapper.selectByExample(deliveryExample);
		
		if (!CollectionUtils.isEmpty(deliveryList)) {
			deliveryList.forEach(o -> {
				billCodes.add(o.getBillNo());
			});
		}
		
		return billCodes;
	}

	/**	
	 * 插入一条 DeliveryPo 数据
	 * @param DeliveryPo 
	 * @return
	 */
	public void insertDeliveryPo(DeliveryPo record) {

		//deliveryMapper.insertSelective(record);

		Logger.comment("成功插入" + deliveryMapper.insertSelective(record) + "条数据");
	}
	
	/**	
	 * 删除delivery数据
	 * @param orderId 
	 * @return	DeliveryPo size
	 */
	public int deleteDeliveryBybillNo(String billNo) {

		deliveryExample.clear();

		deliveryExample.createCriteria().andBillNoEqualTo(billNo);
		
		return deliveryMapper.deleteByExample(deliveryExample);
	}
	
	/**
	 * 获取FinishActivity
	 * @param activityId
	 * @return
	 */
	public FinishActivityPo selectFinishActivityByIdActivityId(Long activityId) {

		this.finishActivityExample.clear();

		finishActivityExample.createCriteria().andActivityIdEqualTo(activityId);

		return Iterables.getFirst(
				this.finishActivityMapper.selectByExample(finishActivityExample), null);
	}
	
	/**
	 * 获取OrderAutoAcceptActivityDetail
	 * 
	 * @param activityId
	 * @param catalogId
	 * @return
	 */
	public OrderAutoAcceptActivityDetailPo selectAutoAcceptActivityDetailByOrderIdAndCatalogId(
			Long orderId, String catalogId) {

		this.orderAutoAcceptActivityDetailExample.clear();

		orderAutoAcceptActivityDetailExample.createCriteria()
				.andOrderIdEqualTo(orderId).andCatalogIdEqualTo(catalogId);

		return Iterables.getFirst(this.orderAutoAcceptActivityDetailMapper
				.selectByExample(orderAutoAcceptActivityDetailExample), null);
	}
	
	public ActualDeleteDeliveryPo selectActualDeleteDeliveryByBillNo(String billNo) {
	
		this.actualDeleteDeliveryExample.clear();
		
		actualDeleteDeliveryExample.createCriteria().andBillNoEqualTo(billNo);
		
		return Iterables.getFirst(this.actualDeleteDeliveryMapper
				.selectByExample(actualDeleteDeliveryExample), null);
	}
	
	public PickUpCodePo selectPickUpCodeByOrderId(Long orderId) {
		
		this.pickUpCodeExample.clear();
		
		pickUpCodeExample.createCriteria().andOrderIdEqualTo(orderId);
		
		return Iterables.getFirst(this.pickUpCodeMapper
				.selectByExample(pickUpCodeExample), null);
	}

	public static void main(String args[]) {

		OrderWapper orderWapper = new OrderWapper();
		
		System.out.println(orderWapper.selectPickUpCodeByOrderId(3927628699L));
		
//		orderWapper.deleteDeliveryBybillNo("9511688614");
		
//		OrderExtPo po = orderWapper.selectOrderExtByOrderId(10374968699L);
//		
//		po.setUserAddress("address");
//		
//		orderWapper.updateAddressByOrderId(po);
//		OrderExtPo po = orderWapper.selectOrderExtByOrderId(3927628699L);
//
//		System.out.println(po.getUserLoginId());
//		System.out.println(po.getUserPhone());
//		
//		orderWapper.selectDeliveryByOrderId(2179038699L);
//		
//		System.out.println(orderWapper.selectDeliveryCodeByOrderId(3757438699L));
		
//		String idcardNo = "520122199005060982";

		/*		IdcardCostPo res = orderWapper
						.selectIdcardCostByIdCardNo("110101195103014545");

				IdcardCostPo record = new IdcardCostPo();

				if (res == null) {

					record.setAmount(BigDecimal.TEN);
					record.setIdcardNo(idCardNo);
					//record.setId(23276L);
					record.setYear(2018);

				} else {
					record.setIdcardNo(idCardNo);
					record.setAmount(BigDecimal.TEN);
					record.setYear(2018);
				}*/

		//orderWapper.idcardCostMapper.updateByPrimaryKey(record);
		//DeliveryPo po = orderWapper.selectDeliveryByOrderId(1836458699);
//		IdcardCostPo record = new IdcardCostPo();
//
//		record.setIdcardNo(idcardNo);
//		record.setYear(2020);
//		record.setAmount(new BigDecimal(10000));
//
//		orderWapper.updatIdCardCostByIdCardNo(record, record.getIdcardNo());

	}
}
